//
//  DetailsViewController.m
//  Cartoon
//
//  Created by dllo on 15/10/29.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "DetailsViewController.h"
#import "DetailsTableViewCell.h"
#import "KindModel.h"
#import "TotalDetailsViewController.h"

@interface DetailsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *arr;
@property(nonatomic, strong)NSMutableDictionary *dic;
@end

@implementation DetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.arr = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[DetailsTableViewCell class] forCellReuseIdentifier:@"details"];
 
    [self json];

}

-(void)json
{
    NSString *str =[NSString stringWithFormat:@"http://mobilev3.ac.qq.com/Classify/comicClassifyDetailForIos/uin/0/local_version/3.1.0/channel/1001/guest_id/943AAD67-2C90-414B-A21F-D9BE0A1E80F6/classify_id/%@/page/1/listcnt/20",self.listId];
        str = [str stringByAddingPercentEncodingWithAllowedCharacters:[ NSCharacterSet URLFragmentAllowedCharacterSet]];

    [NetHandler dataWithUrl:str complant:^(NSData *data) {
        
        self.dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        //        NSLog(@"%@",self.dic);
        NSArray *arr = [self.dic objectForKey:@"data"];
        for (NSDictionary *dict in arr) {
            KindModel *kind = [[KindModel alloc]init];
            [kind setValuesForKeysWithDictionary:dict];
            [self.arr addObject:kind];
        }
        NSLog(@"%@",_arr);
        [self.tableView reloadData];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   KindModel *kind = [self.arr objectAtIndex:indexPath.row];
    DetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"details" forIndexPath:indexPath];
    
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:kind.cover_url]];
    cell.nameLabel.text = kind.title;
    cell.nameLabel.font = [UIFont boldSystemFontOfSize:16];
    cell.authorLabel.text = [NSString stringWithFormat:@"作者:%@",kind.artist_name];
    cell.authorLabel.font = [UIFont systemFontOfSize:14];
    cell.categoryLabel.text = [NSString stringWithFormat:@"类型:%@", kind.type];
    cell.categoryLabel.font = [UIFont systemFontOfSize:14];
    cell.volumeLabel.text = [NSString stringWithFormat:@"更新至%@",kind.lastup];
    cell.volumeLabel.font = [UIFont systemFontOfSize:14];

    cell.timeLabel.text = [NSString stringWithFormat:@"更新:%@",kind.update_date];
    cell.timeLabel.font = [UIFont systemFontOfSize:14];
    cell.gradeLabel.text = [NSString stringWithFormat:@"评分:%@",kind.grade_ave];;
    cell.gradeLabel.font = [UIFont systemFontOfSize:14];
    cell.gradeLabel.textColor = [UIColor redColor];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 150;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    TotalDetailsViewController *totalDetailsV = [[TotalDetailsViewController alloc]init];
    [self.navigationController pushViewController:totalDetailsV animated:YES];
//    KindModel *kind = [self.arr objectAtIndex:indexPath.row];
//    totalDetailsV.comicId = kind.comic_id;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
